define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'socialization/index' + location.search,
                    add_url: 'socialization/add',
                    edit_url: 'socialization/edit',
                    del_url: 'socialization/del',
                    multi_url: 'socialization/multi',
                    table: 'socialization',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: '', title: __('序号'),operate:false,table: table,width:'50px',formatter:Table.api.formatter.natureorder},
                        {field: 'name', title: __('Name'),
                            formatter:function(value,row,index){
                              //此处对value值做判断，不然value为空就会报错
                              if(row.name && row.name.length<=12){
                                return row.name;
                              }
                              if(row.name && row.name.length>12){
                                return row.name.substring(0,11)+"...";
                              }
                            }
                        },
                        {field: 'investment_name', title: __('Investment_name')},
                        {field: 'referrer', title: __('Referrer')},
                        // {field: 'settle_street_ids', title: __('Settle_street_ids')},
                        {field: 'settle_street_names', title: __('Settle_street_ids')},
                        // {field: 'first_registration_time', title: __('First_registration_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'first_registration_images', title: __('First_registration_images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'second_registration_time', title: __('Second_registration_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'second_registration_images', title: __('Second_registration_images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'final_confirm_time', title: __('Final_confirm_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'final_confirm_images', title: __('Final_confirm_images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'reward_application_time', title: __('Reward_application_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'reward_application_images', title: __('Reward_application_images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'is_delete', title: __('Is_delete')},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            var parenttable = table.closest('.bootstrap-table');
            var options = table.bootstrapTable('getOptions');
            var toolbar = $(options.toolbar, parenttable);
            $(toolbar).on('click', ".btn-detail", function () {
                var that = this;
                //循环弹出多个编辑框
                $.each(table.bootstrapTable('getSelections'), function (index, row) {
                    var url = 'socialization/detail';
                    row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                    var url = Table.api.replaceurl(url, row, table);
                    var icon = 'fa fa-info';
                    Backend.api.addtabs(url, '详情-'+row.name, icon);
                });
            });

            $(toolbar).on('click', ".btn-export", function () {
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});
                var filter = search.filter;
                var op = search.op;
                url = 'socialization/export';
                if(url)
                    location.href= url + '?filter='+filter+'&op='+op;
            });


        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
        }
    };
    return Controller;
});